" --------------------------------------------
" --------------- Global Variable ------------
" --------------------------------------------
" if 0 will block all plugin
let g:enable_bundle=1

" only one complete extension to use at same time
let g:youcompleteme_switch=0
let g:neocomplete#enable_at_startup=1

" judge the OS
if(has("win32") || has("win64") || has("win95") || has("win16"))
    let g:system="win"
else
    let g:system="unix" 
endif

" --------------------------------------------
" ------------- Function defination ----------
" --------------------------------------------
func! InitEnv()
    if g:system == 'win'
        set rtp+=$VIM/vimfiles/bundle/Vundle.vim
        set guifont=Consolas:h18
        colorscheme torte
    endif
    if g:system == 'unix'
        set rtp=~/.vim/bundle/Vundle.vim,$VIMRUNTIME
        if(has('gui_running'))
            set gfn=Consolas:h16
            set guifontwide="Hiragino Sans GB" 
            set background=dark
            colorscheme torte
        else
            set t_Co=256
            set background=dark
        endif
    endif
endfu

func! CompileFunc()
    exec "w"
    if &filetype == 'cpp'
        exec "!gcc % -o %<"
        exec "! ./%<"
    elseif &filetype == 'php'
        exec ":! php %"
    elseif &filetype == 'python'
        exec "!python %"
    elseif &filetype == 'sh'
        exec "!sh % %<"
    endif
endfunc

" 初始化bundle插件
func! InitBundle()
    if g:enable_bundle == 1
        " **********bundle相关配置**********************************
        set nocompatible
        filetype off
        call vundle#begin()

        " 管理自身
        Plugin 'gmarik/Vundle.vim'

        "1-> vim-scripts repos插件
        Bundle 'SuperTab'
        Bundle 'The-NERD-tree'
        Bundle 'DoxygenToolkit.vim'
        Bundle 'taglist.vim'

        "2-> github上的人写的插件
        Bundle 'Lokaltog/vim-powerline'
        " Bundle 'jiangmiao/auto-pairs'
        Bundle 'jistr/vim-nerdtree-tabs'
        " Bundle 'Yggdroot/indentLine'
        " Bundle 'morysky/vim-phpcheck'
        Bundle 'shanzi/autoHEADER'
        " Bundle 'maksimr/vim-jsbeautify'
        
        " Need judge plugin
        if v:version >= 730 && g:youcompleteme_switch > 0
            " YCM need 7.3 more
            Bundle 'Valloric/YouCompleteMe'
        endif
        if has('lua') && g:neocomplete#enable_at_startup > 0
            Bundle 'Shougo/neocomplete.vim'
        endif

        " Theme
        Bundle 'tomasr/molokai'

        " instant show the markdown doc
        " Bundle 'isnowfy/python-vim-instant-markdown'

        "3-> 如果是非github插件
        "Bundle 'git://git.wincent.com/command-t.git'

        call vundle#end()            " required
        filetype plugin indent on    " required
    endif
endfunc

func! SetEnv()
endfunc

func! SetBundle()
    if g:enable_bundle == 1
        " ----------------------具体插件配置----------------------------------
        "nerdtree tabs
        let g:nerdtree_tabs_open_on_console_startup=0

        "nerdtree
        let g:NERDTreeIgnore = ['.*\.o$','.*\.ko$','.*\.gz$']
        let g:NERDTreeWinSize = 18
        let g:NERDTreeAutoDeleteBuffer = 1
        let g:NERDTreeWinPos = "left"
        
        " DoxygenToolkit
        let g:DoxygenToolkit_authorName="morysky"

        " php checker
        let g:PHP_SYNTAX_CHECK_BIN = "php"
        let g:php_check_autorun = 0
        
        " header
        let g:autoHEADER_auto_enable = 0
        let g:autoHEADER_default_author = "morysky"
        let g:autoHEADER_fill_char_repeat = 80

        let g:SuperTabDefaultCompletionType="<C-X><C-O>"
        let g:SuperTabRetainCompletionType=1
        let g:SuperTabMappingForward="<tab>"

        " YouCompleteMe configuration
        if v:version >= 700 && g:youcompleteme_switch > 0
            let g:ycm_global_ycm_extra_conf = '~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py'
        endif

        " taglist
        let Tlist_Use_Horiz_Window=1

        " powerline
        let g:Powerline_stl_path_style = 'full'

        " 弹出框相关的键配置
        inoremap <expr> <CR>       pumvisible() ? "\<C-y>" : "\<CR>"

        " plugin map
        map wm      <plug>NERDTreeTabsToggle<CR>
        map <leader>il :IndentLinesToggle<CR>
    endif
endfunc

func! SetColorScheme()
    try 
        let g:molokai_original = 1
        colorscheme molokai
    catch /^Vim\%((\a\+)\)\=:E185/
        " if molokai do not exist
        colorscheme desert
    endtry
endfunc

" --------------------------------------------
" ------------ General configuration ---------
" --------------------------------------------
set history=500

set ruler

set cmdheight=3

" auto load when a file is changed from the outside
set autoread

" 设置鼠标模式
" c 命令行模式，可以直接复制粘贴,就像在终端中一样
" a 所有模式，可以做诸如点击Tlist的tag，鼠标拖拉窗口等操作
set mouse=c

" 解决中文显示一半的问题
set ambiwidth=double

set smarttab

" enconding guess list
set fencs=utf-8,cp936
set fileencodings=utf-8,cp936
set termencoding=utf-8

set nobackup
set nowritebackup
set noswapfile
set nowb

" match the bracket
set showmatch

syntax enable

" Custom filetype check
au! BufNewFile,BufRead *.vue setf html

" regex stuff
set completeopt=longest,menu
set magic
" magic模式下 除 $ . * ^ 外需要加反斜杠
" nomagic 除$ ^ 外需要加反斜杠

set number

" highlight
set cursorline
set cursorcolumn

set incsearch
set hlsearch

set backspace=indent,eol,start
" set smartindent
set tabstop=4 expandtab
" auto indent tab size
set shiftwidth=4

" auto omnifunc auto complete
set omnifunc=syntaxcomplete#Complete
autocmd FileType php set omnifunc=phpcomplete#CompletePHP
autocmd FileType c set omnifunc=ccomplete#Complete
set tags=./tags,./TAGS,tags;~,TAGS;~

set autoindent
set smartindent
set wrap

set laststatus=2

" change dir to current dir
au BufRead,BufNewFile,BufEnter * cd %:p:h

set colorcolumn=110
highlight ColorColumn ctermbg=darkgray

" u got a fast terminal
" set ttyfast " this could stop neo complete work
set ttyscroll=3
set lazyredraw "

set nocursorcolumn
set nocursorline
set norelativenumber
syntax sync minlines=256

" --------------------------------------------
" ----------------- Hot key ------------------
" --------------------------------------------
map :run    :call CompileFunc() <CR>
nmap tn     :tabnew <CR>
"前一个标签，后一个标签
nmap tr     :tabp <CR>
nmap ty     :tabn <CR>

" tag list
nmap tg     :TlistToggle <CR>

" --------------------------------------------
" --------------- Main process ---------------
" --------------------------------------------
call InitEnv()
call InitBundle()

call SetEnv()
call SetBundle()
call SetColorScheme()
